<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property string $id
 * @property string $category_id
 * @property string $name
 * @property integer $status
 * @property string $tax_id
 * @property string $image
 * @property string $description
 * @property integer $created_time
 * @property integer $created_by
 * @property integer $modified_time
 * @property integer $modified_by
 */
class Product extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Product the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'product';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('category_id, name, status, tax_id, created_time, created_by', 'required'),
            array('status, created_time, created_by, modified_time, modified_by', 'numerical', 'integerOnly' => true),
            array('category_id, tax_id', 'length', 'max' => 4),
            array('name', 'length', 'max' => 255),
            array('description', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, category_id, name, status, tax_id, description, created_time, created_by, modified_time, modified_by', 'safe', 'on' => 'search'),
            array('image', 'file',
                'types' => 'gif, jpg, png',
                'maxSize' => 1024 * 1024 * 2,
                'tooLarge' => 'The file was larger than 2MB. Please upload a smaller file.',
                'allowEmpty' => true,
            )
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        return array(
            'orders' => array(self::MANY_MANY, 'Order', 'ShopProductOrder(order_id, product_id)'),
            'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
            'tax' => array(self::BELONGS_TO, 'Tax', 'tax_id'),
            'images' => array(self::HAS_MANY, 'ProductImage', 'product_id'),
            'shopping_carts' => array(self::HAS_MANY, 'ShoppingCart', 'product_id'),
            'supplier_product' => array(self::HAS_MANY, 'SupplierProduct', 'product_id'),
            'distributors' => array(self::HAS_MANY, 'DistributorProduct', 'product_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'category_id' => 'Category',
            'name' => 'Name',
            'status' => 'Status',
            'tax_id' => 'Tax (%)',
            'image' => 'Image',
            'description' => 'Description',
            'created_time' => 'Created Time',
            'created_by' => 'Created By',
            'modified_time' => 'Modified Time',
            'modified_by' => 'Modified By',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search(array $time = null) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria = new CDbCriteria;
        $user = Users::model()->findByPk(Yii::app()->user->id);
        if ($user->superuser < 5)
            $criteria->addCondition('created_by = ' . Yii::app()->user->id);
        $criteria->compare('id', $this->id, true);
        $criteria->compare('category_id', $this->category_id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('description', $this->description, true);

        $criteria->compare('created_by', $this->created_by);

        $criteria->compare('modified_by', $this->modified_by);
        //$criteria->join = 'INNER JOIN tax ON tax.id=tax_id';
        //$criteria->addCondition('created_by = '.Yii::app()->user->id);
//        if ($time['created_from'] != null){
//            $criteria->addCondition('t.created_time>'.$time['created_from']);
//        }
//        $criteria->compare('t.created_time', '<'. $time['created_to']);
//        if ($time['modified_from'] != null){
//            $criteria->addCondition('t.modified_time>'.$time['modified_from']);
//        }
//        if ($time['modified_to'] != null){
//            $criteria->addCondition('t.modified_time<'.$time['modified_to']);
//        }
        $criteria->compare('percent', $this->tax_id);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    public function getImagePath() {
        if ($this->image == null)
            return Yii::app()->getRequest()->getBaseUrl(true) . '/protected/medias/product/images/no-pic.jpg';
        return Yii::app()->getRequest()->getBaseUrl(true) . '/protected/medias/product/images/' . $this->image;
    }

    public function getImage($image = 0, $thumb = false) {
        if (isset($this->images[$image]))
            return Yii::app()->controller->renderPartial('/image/view', array(
                'model' => $this->images[$image],
                'thumb' => $thumb), true);
    }

    public static function listProduct() {
        $products = self::model()->findAll();
        $arr = array();
        foreach ($products as $product) {
            $arr[$product->id] = Category::model()->findByPk($product->category_id)->name . ' > ' . $product->name;
        }
        return $arr;
    }
    public static function limitDesc($desc,$limit = 200) {
    
                $summary = $desc;
                if (strlen($summary) > $limit)
                $summary = substr($summary, 0, strrpos(substr($summary, 0, $limit), ' ')) . '...';
            return $summary;
    }

}